#include "qalarm_p.h"
#include <ASCliSession.h>
#include <ASShdAlarm.h>
#include <ASShdDefs.h>

// http://www.developer.nokia.com/Community/Wiki/CalInterimAPI

QAlarmPrivate::QAlarmPrivate(QAlarm* aPublicAPI): iPublicAlarm(aPublicAPI)
{
}

QAlarmPrivate::~QAlarmPrivate()
{
    delete iPublicAlarm;
}

//int QAlarmPrivate::add(QString message, QAlarm::Type type){
int QAlarmPrivate::add(QString message, int interval){

    TPtrC syMessage (static_cast<const TUint16*>(message.utf16()), message.length());

    /*TASShdAlarm alarm;

    alarm.Category() = KASCliCategoryClock;
    alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatDaily;

    TPtrC syMessage (static_cast<const TUint16*>(message.utf16()), message.length());

    //_LIT( KAlarmTestInit, "This is an alarm message - alarm 1" );
    //alarm.Message() = KAlarmTestInit;
    alarm.Message() = syMessage;
    alarm.NextDueTime().HomeTime();
    alarm.NextDueTime() += TTimeIntervalMinutes( 1 );

    /*_LIT( KAlarmTestSoundName, "Arf Arf Woop" );
    alarm.SoundName() = KAlarmTestSoundName;*/

    /*TInt ret = TheAlarmServerSession.AlarmAdd( alarm );

    //TAlarmId alarmId = alarm.Id();

    return alarm.Id();*/

    RASCliSession iSession;  // client interface to alarm server
    iSession.Connect();
    TASShdAlarm iAlarm;	//Alarm object

    TTime ihomeTime;
    ihomeTime.HomeTime(); // Get Current Hometime(local time set in mobile)

    //Tint  interval;
    // interval � Give seconds after which alarm should expire
    TTimeIntervalSeconds intervalSecs(interval);

    //Returns the time alarm is scheduled to expire
    TTime& iNDT = iAlarm.NextDueTime();

    //Set the NextDueTime in which alarm will expire
    iNDT = ihomeTime + intervalSecs;

    TAlarmMessage& imessage(iAlarm.Message());
    //imessage.Copy(_L("Alarm")); //Alarm message
    imessage.Copy(syMessage); //Alarm message

    iSession.AlarmAdd(iAlarm);
    iSession.Close();

}
